package com.hamizhong.iflowers.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.hamizhong.iflowers.db.dao.hibernate.IVarietyDao;
import com.hamizhong.iflowers.db.entity.Variety;
import com.hamizhong.iflowers.service.IVarietyService;
import com.hamizhong.iflowers.service.base.MyBaseService;
import com.hamizhong.iflowers.utils.MyErrorMsgConstants;
import com.hamizhong.iflowers.web.dto.IOPResultDto;
import com.hamizhong.iflowers.web.vo.IObjectVO;
import com.hamizhong.iflowers.web.vo.VarietyVO;

public class VarietyServiceImpl extends MyBaseService implements IVarietyService {
	private IVarietyDao iVarietyDao = null;

	public IVarietyDao getiVarietyDao() {
		return iVarietyDao;
	}
	public void setiVarietyDao(IVarietyDao iVarietyDao) {
		this.iVarietyDao = iVarietyDao;
	}
	
	public IOPResultDto<IObjectVO> findAll(Integer pageNum, int pageSize) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		List<Variety> varietyList = new ArrayList<Variety>();
		varietyList = iVarietyDao.findVariety(pageNum, pageSize);
		int totalVarietyCnt = iVarietyDao.findTotalVarietyCnt();
		List<VarietyVO> varietyVOList = new ArrayList<VarietyVO>();
		VarietyVO varietyVO = null;
		for (Variety variety : varietyList) {
			varietyVO = new VarietyVO();
			BeanUtils.copyProperties(variety, varietyVO);
			varietyVOList.add(varietyVO);
		}
		IObjectVO iObjectVO = new IObjectVO();
		iObjectVO.setVarietyVOList(varietyVOList);
		iObjectVO.setTotalVarietyCnt(totalVarietyCnt);
		dto.addToResultList(iObjectVO);
		dto.setSuccess(true);
		return dto;
	}
	public IOPResultDto<IObjectVO> addVariety(VarietyVO varietyVO) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		Variety variety = new Variety();
		try {
			BeanUtils.copyProperties(varietyVO, variety);
			iVarietyDao.add(variety);
			dto.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			dto.setSuccess(false);
			dto.setErrorMSG(MyErrorMsgConstants.FAILED_TO_ADD_OPERATE);
		}
		return dto;
	}
	public IOPResultDto<IObjectVO> updateVariety(VarietyVO varietyVO) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		Variety variety = new Variety();
		try {
			BeanUtils.copyProperties(varietyVO, variety);
			iVarietyDao.update(variety);
			dto.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			dto.setSuccess(false);
			dto.setErrorMSG(MyErrorMsgConstants.FAILED_TO_UPDATE_OPERATE);
		}
		return dto;
	}
	public IOPResultDto<IObjectVO> deleteVariety(VarietyVO varietyVO) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		try {
			String varietyId = varietyVO.getVarietyId();
			Variety variety = iVarietyDao.findById(varietyId);
			if(null != variety) {
				this.iVarietyDao.delete(variety);
				dto.setSuccess(true);
			}else {
				dto.setSuccess(false);
				dto.setErrorMSG(MyErrorMsgConstants.NOT_EXIST_DATA_FOR_OPERATE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			dto.setErrorMSG(MyErrorMsgConstants.ERR_SYSTEM);
			dto.setSuccess(false);
		}
		return dto;
	}
	
}
